package com.dingwei.superiordiary.mvp.presenter;

import com.dingwei.superiordiary.base.MyBaseBean;
import com.dingwei.superiordiary.bean.BrowseHistoryBean;
import com.dingwei.superiordiary.bean.CollectListBean;
import com.dingwei.superiordiary.bean.FailsBean;
import com.dingwei.superiordiary.custom.myCallback;
import com.dingwei.superiordiary.mvp.view.Contract;
import com.lihao.baselibrary.base.BaseActivity;
import com.lihao.baselibrary.base.URLS;
import com.lihao.baselibrary.base.basePresenter;

import org.xutils.http.RequestParams;
import org.xutils.x;

/**
 * Created by lihao on 2017/9/12.
 */

public class MyCollectionPresenter extends basePresenter {

    private Contract.MyCollectionView myCollectionView;

    public MyCollectionPresenter(Contract.MyCollectionView view) {
        super(view);
        this.myCollectionView = view;
    }
    public void initData(int page, final boolean isRefresh, boolean isShowDiaolg) {

        RequestParams rp = new RequestParams(URLS.HOST + URLS.COLLECTION_LIST);
        rp.addBodyParameter("sessionid", BaseActivity.value.getToken());
        rp.addBodyParameter("page", page + "");
        x.http().post(rp, new myCallback(BaseActivity.context, CollectListBean.class, isShowDiaolg, rp) {
            @Override
            public void back(MyBaseBean baseBean, String result) {
                CollectListBean bean = (CollectListBean) baseBean;
                if (isRefresh) {
                    myCollectionView.onRefresh(bean);
                } else {
                    myCollectionView.onLoadMore(bean);
                }
            }

            @Override
            public void fail(FailsBean baseBean, String result) {
                myCollectionView.onFail();
            }
        });
    }

    public void delete(String id, final int position) {

        RequestParams rp = new RequestParams(URLS.HOST + URLS.DELETE_COLLECTION);
        rp.addBodyParameter("sessionid", BaseActivity.value.getToken());
        rp.addBodyParameter("id", id);
        x.http().post(rp, new myCallback(BaseActivity.context, MyBaseBean.class, rp) {
            @Override
            public void back(MyBaseBean baseBean, String result) {
                showToast(baseBean.message);
                myCollectionView.deleteSuccess(position);
            }

            @Override
            public void fail(FailsBean baseBean, String result) {
            }
        });
    }
}
